package org.jeecg.modules.farming.mini;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.common.constant.CommonConstant;

import java.io.Serializable;

/**
 *   接口返回数据格式
 * @author Samiu
 * @email 13798974393@163.com
 * @date  2020年12月12日
 */
@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class MiniResult<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 返回处理消息
	 */
	@ApiModelProperty(value = "返回处理消息")
	private String msg = "SUCCESS";

	/**
	 * 返回代码
	 */
	@ApiModelProperty(value = "返回代码")
	private Integer code = 0;

	/**
	 * 返回行数
	 */
	@ApiModelProperty(value = "返回行数")
	private Integer count = 0;

	/**
	 * 返回数据对象 data
	 */
	@ApiModelProperty(value = "返回数据对象")
	private T data;

	public MiniResult() {
		
	}
	
	public MiniResult<T> success(String message) {
		this.msg = message;
		this.code = 0;
		return this;
	}

	@Deprecated
	public static MiniResult<Object> ok() {
		MiniResult<Object> r = new MiniResult<Object>();
		r.setCode(0);
		r.setMsg("SUCCESS");
		return r;
	}

	@Deprecated
	public static MiniResult<Object> ok(String msg) {
		MiniResult<Object> r = new MiniResult<Object>();
		r.setCode(0);
		r.setMsg(msg);
		return r;
	}

	@Deprecated
	public static MiniResult<Object> ok(Object data) {
		MiniResult<Object> r = new MiniResult<Object>();
		r.setCode(0);
		r.setData(data);
		return r;
	}

	public static<T> MiniResult<T> OK() {
		MiniResult<T> r = new MiniResult<T>();
		r.setCode(0);
		r.setMsg("SUCCESS");
		return r;
	}

	public static<T> MiniResult<T> OK(T data) {
		MiniResult<T> r = new MiniResult<T>();
		r.setCode(0);
		r.setCount(1);
		r.setData(data);
		return r;
	}

	public static<T> MiniResult<T> OK(String msg, T data) {
		MiniResult<T> r = new MiniResult<T>();
		r.setCode(0);
		r.setMsg(msg);
		r.setData(data);
		return r;
	}
	
	public static MiniResult<Object> error(String msg) {
		return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
	}
	
	public static MiniResult<Object> error(int code, String msg) {
		MiniResult<Object> r = new MiniResult<Object>();
		r.setCode(code);
		r.setMsg(msg);
		return r;
	}

	public MiniResult<T> error500(String message) {
		this.msg = message;
		this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
		return this;
	}
	/**
	 * 无权限访问返回结果
	 */
	public static MiniResult<Object> noauth(String msg) {
		return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
	}

	@JsonIgnore
	private String onlTable;

}